#!/bin/bash
#
# Copyright (C) 2014 David Goulet <dgoulet@efficios.com>
#
# SPDX-License-Identifier: LGPL-2.1-only

TEST_DESC="Load session(s)"

CURDIR=$(dirname $0)/
CONFIG_DIR="${CURDIR}/configuration"
TESTDIR=$CURDIR/../../../
export LTTNG_SESSION_CONFIG_XSD_PATH=$(readlink -m ${TESTDIR}../src/common/)

SESSION_NAME="load-42"
EVENT_NAME="tp:tptest"

DIR=$(readlink -f $TESTDIR)

NUM_TESTS=75

source $TESTDIR/utils/utils.sh

# MUST set TESTDIR before calling those functions
plan_tests $NUM_TESTS

print_test_banner "$TEST_DESC"

function test_basic_load()
{
	diag "Test basic load"

	lttng_load_ok "-i $CURDIR/$SESSION_NAME.lttng"

	destroy_lttng_session_ok $SESSION_NAME
}

function test_complex_load()
{
	local sess="$SESSION_NAME-complex"
	local mi_result=""
	local ret=1
	diag "Test complex load"

	# Start relayd with localhost binding. The complex session uses those
	# custom values.
	start_lttng_relayd "-C tcp://localhost:8172 -D tcp://localhost:9817"

	lttng_load_ok "-i $CURDIR/$sess.lttng"

	# Once loaded, we are suppose to be able to disable certain events/channels
	# thus having a confirmation that it's valid
	disable_ust_lttng_event $sess uevent1 chan1
	disable_ust_lttng_event $sess uevent2 chan2
	disable_ust_lttng_event $sess uevent3* chan3

	disable_ust_lttng_channel $sess chan1
	disable_ust_lttng_channel $sess chan2
	disable_ust_lttng_channel $sess chan3

	# Confirm that an event stored as disabled is restored in its disabled state
	local mi_output_file=$(mktemp -t "tmp.${FUNCNAME[0]}_output_file.XXXXXX")
	if [ $? -ne 0 ]; then
		break;
	fi
	$TESTDIR/../src/bin/lttng/$LTTNG_BIN --mi XML list $sess -c chan2 > $mi_output_file
	mi_result=$("$XML_EXTRACT" $mi_output_file "//lttng:command/lttng:output/lttng:sessions/lttng:session/lttng:domains/lttng:domain[lttng:type='UST']/lttng:channels/lttng:channel[lttng:name='chan2']/lttng:events/lttng:event[lttng:name='uevent_disabled']/lttng:enabled/text()")
	if [[ $mi_result = "false" ]]; then
	    ok 0 "Disabled event is loaded in disabled state"
	else
	    fail "Disabled event is loaded in disabled state"
	fi

	mi_result=$("$XML_EXTRACT" $mi_output_file "//lttng:command/lttng:output/lttng:sessions/lttng:session/lttng:domains/lttng:domain/lttng:channels/lttng:channel[lttng:name='chan2']/lttng:events/lttng:event[lttng:name='uevent_disabled']/lttng:enabled/text()")
	test $mi_result = "false"
	ok $? "Disabled event is loaded in disabled state"

	# Check that uevent_same_name_diff_llevel with log level 6 (TRACE_INFO) is enabled
	# This ensure that the state of events with similar name but not same
	# descriptor tuple (exclusion,filter,loglevel) is restored correctly.
	mi_result=$("$XML_EXTRACT" $mi_output_file "//lttng:command/lttng:output/lttng:sessions/lttng:session/lttng:domains/lttng:domain/lttng:channels/lttng:channel[lttng:name='chan2']/lttng:events/lttng:event[lttng:name='uevent_same_name_diff_llevel' and lttng:loglevel='TRACE_INFO']/lttng:enabled/text()")
	test $mi_result = "true"
	ok $?  "Enabled event with same name but different loglevel is in disabled state"

	destroy_lttng_session_ok $sess

	rm $mi_output_file
	stop_lttng_relayd
	rm -rf ${mi_output_file}
}

function test_all_load()
{
	diag "Test load all sessions"

	# Start relayd with localhost binding. The complex session uses those
	# custom values.
	start_lttng_relayd "-C tcp://localhost:8172 -D tcp://localhost:9817"

	lttng_load_ok "-a -i $CURDIR"

	destroy_lttng_session_ok $SESSION_NAME
	destroy_lttng_session_ok "$SESSION_NAME-complex"
	destroy_lttng_session_ok "$SESSION_NAME-trackers"
	destroy_lttng_session_ok "tracker_legacy_all"
	destroy_lttng_session_ok "tracker_legacy_none"
	destroy_lttng_session_ok "tracker_legacy_selective"

	stop_lttng_relayd
}

function test_overwrite()
{
	diag "Test load overwrite"

	lttng_load_ok "-i $CURDIR/$SESSION_NAME.lttng"

	# This one should succeed
	lttng_load_ok "-f -i $CURDIR $SESSION_NAME"

	destroy_lttng_session_ok $SESSION_NAME
}

function test_trackers()
{
	diag "Test trackers loading"

	lttng_load_ok "-i $CURDIR/$SESSION_NAME-trackers.lttng"

	diag "Test pid tracker"
	local mi_output_file=$(mktemp -t "tmp.${FUNCNAME[0]}_output_file.XXXXXX")
	if [ $? -ne 0 ]; then
		break;
	fi
	$TESTDIR/../src/bin/lttng/$LTTNG_BIN --mi XML list "$SESSION_NAME-trackers" > $mi_output_file
	mi_result=$($XML_NODE_CHECK $mi_output_file "//lttng:command/lttng:output/lttng:sessions/lttng:session/lttng:domains/lttng:domain/lttng:process_attr_trackers/lttng:vpid_process_attr_tracker/lttng:process_attr_values/lttng:vpid")
	if [[ $mi_result = "true" ]]; then
	    ok 0 "VPID target is present"
	else
	    fail "VPID target missing"
	fi

	mi_result=$($XML_NODE_CHECK $mi_output_file "//lttng:command/lttng:output/lttng:sessions/lttng:session/lttng:domains/lttng:domain/lttng:process_attr_trackers/lttng:vuid_process_attr_tracker/lttng:process_attr_values/lttng:vuid")
	if [[ $mi_result = "true" ]]; then
	    ok 0 "VUID target is present"
	else
	    fail "VUID target missing"
	fi

	mi_result=$($XML_NODE_CHECK $mi_output_file "//lttng:command/lttng:output/lttng:sessions/lttng:session/lttng:domains/lttng:domain/lttng:process_attr_trackers/lttng:vgid_process_attr_tracker/lttng:process_attr_values/lttng:vgid")
	if [[ $mi_result = "true" ]]; then
	    ok 0 "VGID target is present"
	else
	    fail "VGID target missing"
	fi

	# Test to remove the target just to make sure
	lttng_untrack_ok "--vpid 666 -u -s $SESSION_NAME-trackers"
	lttng_untrack_ok "--vuid 777 -u -s $SESSION_NAME-trackers"
	lttng_untrack_ok "--vgid 888 -u -s $SESSION_NAME-trackers"
	# Actually using vpid (backward compat)
	lttng_untrack_ok "--pid 999 -u -s $SESSION_NAME-trackers"

	destroy_lttng_session_ok "$SESSION_NAME-trackers"
	rm -f ${mi_output_file}
}

function test_override_url_normal()
{
	local local_url_override="file:///tmp/override/to/here"
	local local_path_override="/tmp/override/to/here"
	local local_path_compare_value="/tmp/override/to/here"
	local stream_url_override="net://127.0.0.1:8172:9817"
	local stream_url_compare="tcp4://127.0.0.1:8172/ [data: 9817]"

	diag "Test url override for normal session"

	start_lttng_relayd "-C tcp://127.0.0.1:8172 -D tcp://127.0.0.1:9817"

	local mi_output_file=$(mktemp -t "tmp.${FUNCNAME[0]}_output_file.XXXXXX")
	if [ $? -ne 0 ]; then
		break;
	fi

	# Url of style file://
	lttng_load_ok "-i $CURDIR/$SESSION_NAME.lttng --override-url=${local_url_override}"
	$TESTDIR/../src/bin/lttng/$LTTNG_BIN --mi XML list "$SESSION_NAME" > $mi_output_file
	mi_result=$("$XML_EXTRACT" $mi_output_file "//lttng:command/lttng:output/lttng:sessions/lttng:session/lttng:path")

	test $mi_result = "${local_path_compare_value}"
	ok $? "Path url file:// override [{$mi_result}, ${local_path_compare_value}]"
	destroy_lttng_session_ok "$SESSION_NAME"

	# Url of style /
	lttng_load_ok "-i $CURDIR/$SESSION_NAME.lttng --override-url=${local_path_override}"
	$TESTDIR/../src/bin/lttng/$LTTNG_BIN --mi XML list "$SESSION_NAME" > $mi_output_file
	mi_result=$("$XML_EXTRACT" $mi_output_file "//lttng:command/lttng:output/lttng:sessions/lttng:session/lttng:path")

	test $mi_result = "${local_path_compare_value}"
	ok $? "Path url / override [{$mi_result}, ${local_path_compare_value}]"
	destroy_lttng_session_ok "$SESSION_NAME"

	# Url of style net://ip:port:port
	lttng_load_ok "-i $CURDIR/$SESSION_NAME.lttng --override-url=${stream_url_override}"
	$TESTDIR/../src/bin/lttng/$LTTNG_BIN --mi XML list "$SESSION_NAME" > $mi_output_file
	mi_result=$("$XML_EXTRACT" $mi_output_file "//lttng:command/lttng:output/lttng:sessions/lttng:session/lttng:path")

	test "$mi_result" = "${stream_url_compare}"
	ok $? "Path url net://host:port:port override [${mi_result}, ${stream_url_compare}]"

	destroy_lttng_session_ok "$SESSION_NAME"
	stop_lttng_relayd
	rm -rf ${mi_output_file}
}

function test_override_url_snapshot()
{
	local url_override="file:///tmp/override/to/here"
	local path_override="/tmp/override/to/here"
	local path_compare_value="/tmp/override/to/here"
	local stream_url_override="net://127.0.0.1:8172:9817"
	local stream_url_compare_ctrl="tcp4://127.0.0.1:8172/"
	local stream_url_compare_data="tcp4://127.0.0.1:9817/"
	local local_session_name="${SESSION_NAME}-snapshot"

	diag "Test url override for snapshot session"

	start_lttng_relayd "-C tcp://127.0.0.1:8172 -D tcp://127.0.0.1:9817"

	local mi_output_file=$(mktemp -t "tmp.${FUNCNAME[0]}_output_file.XXXXXX")
	if [ $? -ne 0 ]; then
		break;
	fi

	# Url of style file://
	lttng_load_ok "-i $CONFIG_DIR/$local_session_name.lttng --override-url=${url_override}"
	$TESTDIR/../src/bin/lttng/$LTTNG_BIN --mi XML snapshot list-output -s "$local_session_name" > $mi_output_file
	mi_result=$("$XML_EXTRACT" $mi_output_file "//lttng:command/lttng:output/lttng:snapshot_action/lttng:output/lttng:session/lttng:snapshots/lttng:snapshot/lttng:ctrl_url")

	test $mi_result = "${path_compare_value}"
	ok $? "Path url file:// override [{$mi_result}, ${path_compare_value}]"
	destroy_lttng_session_ok "$local_session_name"

	# Url of style /
	lttng_load_ok "-i $CONFIG_DIR/$local_session_name.lttng --override-url=${path_override}"
	$TESTDIR/../src/bin/lttng/$LTTNG_BIN --mi XML snapshot list-output -s "$local_session_name" > $mi_output_file
	mi_result=$("$XML_EXTRACT" $mi_output_file "//lttng:command/lttng:output/lttng:snapshot_action/lttng:output/lttng:session/lttng:snapshots/lttng:snapshot/lttng:ctrl_url")

	test $mi_result = "${path_compare_value}"
	ok $? "Path url / override [{$mi_result}, ${path_compare_value}]"
	destroy_lttng_session_ok "$local_session_name"

	# Url of style net://ip:port:port
	lttng_load_ok "-i $CONFIG_DIR/$local_session_name.lttng --override-url=${stream_url_override}"
	$TESTDIR/../src/bin/lttng/$LTTNG_BIN --mi XML snapshot list-output -s "$local_session_name" > $mi_output_file
	mi_result=$("$XML_EXTRACT" $mi_output_file "//lttng:command/lttng:output/lttng:snapshot_action/lttng:output/lttng:session/lttng:snapshots/lttng:snapshot/lttng:ctrl_url")

	test "$mi_result" = "${stream_url_compare_ctrl}"
	ok $? "Path url ctrl net://host:port:port override [${mi_result}, ${stream_url_compare_ctrl}]"

	mi_result=$("$XML_EXTRACT" $mi_output_file "//lttng:command/lttng:output/lttng:snapshot_action/lttng:output/lttng:session/lttng:snapshots/lttng:snapshot/lttng:data_url")

	test "$mi_result" = "${stream_url_compare_data}"
	ok $? "Path url data net://host:port:port override [${mi_result}, ${stream_url_compare_data}]"

	destroy_lttng_session_ok "$local_session_name"
	stop_lttng_relayd
	rm -rf ${mi_output_file}
}

function test_override_url_live()
{
	local url_override="file:///tmp/override/to/here"
	local path_override="/tmp/override/to/here"
	local path_compare_value="/tmp/override/to/here"
	local stream_url_override="net://127.0.0.1:8172:9817"
	local stream_url_compare="tcp4://127.0.0.1:8172/ [data: 9817]"
	local local_session_name="${SESSION_NAME}-live"

	diag "Test url override for live session"

	start_lttng_relayd "-C tcp://127.0.0.1:8172 -D tcp://127.0.0.1:9817"

	local mi_output_file=$(mktemp -t "tmp.${FUNCNAME[0]}_output_file.XXXXXX")
	if [ $? -ne 0 ]; then
		break;
	fi

	# Url of style file://
	# Expect this to fail since live session should never accept local
	# output.
	lttng_load_fail "-i $CONFIG_DIR/$local_session_name.lttng --override-url=${url_override}"
	destroy_lttng_session_fail "$local_session_name"

	# Url of style /
	# Expect this to fail since live session should never accept local
	# output.
	lttng_load_fail "-i $CONFIG_DIR/$local_session_name.lttng --override-url=${path_override}"
	destroy_lttng_session_fail "$local_session_name"

	# Url of style net://ip:port:port
	lttng_load_ok "-i $CONFIG_DIR/$local_session_name.lttng --override-url=${stream_url_override}"
	$TESTDIR/../src/bin/lttng/$LTTNG_BIN --mi XML list "$local_session_name" > $mi_output_file
	mi_result=$("$XML_EXTRACT" $mi_output_file "//lttng:command/lttng:output/lttng:sessions/lttng:session/lttng:path")

	test "$mi_result" = "${stream_url_compare}"
	ok $? "Path url net://host:port:port override [${mi_result}, ${stream_url_compare}]"

	destroy_lttng_session_ok "$local_session_name"
	stop_lttng_relayd
	rm -rf ${mi_output_file}
}

function test_override_session_name()
{
	diag "Test session name override"
	local override_name="PinkyAndTheBrain"
	local output_path="/tmp/lttng/load-42-1"

	local mi_output_file=$(mktemp -t "tmp.${FUNCNAME[0]}_output_file.XXXXXX")
	if [ $? -ne 0 ]; then
		break;
	fi

	lttng_load_ok "-i ${CURDIR}/${SESSION_NAME}.lttng --override-name=${override_name} ${SESSION_NAME}"
	${TESTDIR}/../src/bin/lttng/${LTTNG_BIN} --mi XML list "${SESSION_NAME}" &> /dev/null
	if [ $? -eq 0 ]; then
		fail "Expected a failure on listing for old session name"
	fi

	${TESTDIR}/../src/bin/lttng/${LTTNG_BIN} --mi XML list "${override_name}" > $mi_output_file
	mi_result=$("$XML_EXTRACT" $mi_output_file "//lttng:command/lttng:output/lttng:sessions/lttng:session/lttng:name")

	test "${mi_result}" = "${override_name}"
	ok $? "Override name successful  [${SESSION_NAME} to ${override_name}]"

	# Make sure that the name override did not change something else
	mi_result=$("$XML_EXTRACT" $mi_output_file "//lttng:command/lttng:output/lttng:sessions/lttng:session/lttng:path")
	test "${mi_result}" = "${output_path}"
	ok $? "Output path is not affected by name override"

	destroy_lttng_session_ok "${override_name}"

	diag "Test session name override with all (Do not permit)"
	lttng_load_fail "-i ${CURDIR}/${SESSION_NAME}.lttng --override-name=${override_name}"

	rm -rf ${mi_output_file}
}

start_lttng_sessiond

TESTS=(
	test_basic_load
	test_complex_load
	test_all_load
	test_overwrite
	test_trackers
	test_override_session_name
	test_override_url_normal
	test_override_url_snapshot
	test_override_url_live
)

for fct_test in ${TESTS[@]};
do
	TRACE_PATH=$(mktemp -d -t tmp.test_load_trace_path.XXXXXX)

	${fct_test}
	if [ $? -ne 0 ]; then
		break;
	fi
	# Only delete if successful
	rm -rf $TRACE_PATH
done

stop_lttng_sessiond
